package cloud.lysky.game.gamesampe.entity;

import cloud.lysky.game.gamesampe.component.HealthComponent;
import cloud.lysky.game.gamesampe.component.StatsComponent;
import cloud.lysky.game.gamesampe.constant.ItemType;
import cloud.lysky.game.gamesampe.constant.Rarity;
import cloud.lysky.game.gamesampe.model.DamageResult;
import cloud.lysky.game.gamesampe.util.GameUtil;
import com.almasb.fxgl.entity.Entity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.function.TriFunction;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;

@Schema(description = "技能类")
@Getter
@Setter
public class Skill extends Item {

    @Schema(description = "最低伤害")
    private int lowATK;

    @Schema(description = "最高伤害")
    private int highATK;

    @Schema(description = "法力值消耗量")
    private int consumption;

    @Schema(description = "技能特殊效果")
    private TriFunction<Entity, Entity, DamageResult, Integer> effect;

    public Skill(){
        setItemType(ItemType.SKILL);
        setRarity(Rarity.COMMON);
    }

    public static Skill basic(String name) {
        Skill basicSkill = new Skill();
        basicSkill.setCode("001");
        basicSkill.setHighATK(3);
        basicSkill.setLowATK(1);
        basicSkill.setLevel(1);
        basicSkill.setName(StringUtils.isBlank(name) ? "普通攻击" : name);
        basicSkill.setDescription("平凡的一击");
        basicSkill.setConsumption(0);
        return basicSkill;
    }

    public static Skill advance() {
        Skill basicSkill = new Skill();
        basicSkill.setCode("002");
        basicSkill.setHighATK(31);
        basicSkill.setLowATK(20);
        basicSkill.setLevel(1);
        basicSkill.setName("绝命火焰");
        basicSkill.setDescription("命中时，有5%的概率造成对方最大生命值16%的伤害");
        basicSkill.setConsumption(0);
        basicSkill.setEffect((attack, defense, damageResult) -> {
            boolean eventHappen = GameUtil.isEventHappen(0.05);
            if (eventHappen) {
                HealthComponent healthComponent = defense.getComponent(HealthComponent.class);
                return new BigDecimal(healthComponent.getMaxHP()).multiply(new BigDecimal("0.8")).setScale(0, RoundingMode.CEILING).intValue();
            }
            return 0;
        });
        return basicSkill;
    }

    public static Skill advance2() {
        Skill basicSkill = new Skill();
        basicSkill.setCode("004");
        basicSkill.setHighATK(55);
        basicSkill.setLowATK(22);
        basicSkill.setLevel(1);
        basicSkill.setName("吸星大法");
        basicSkill.setDescription("吸取造成伤害的60%为生命值");
        basicSkill.setConsumption(0);
        basicSkill.setEffect((attack, defense, damageResult) -> {
            if (damageResult == null) {
                return 0;
            }
            HealthComponent component = attack.getComponent(HealthComponent.class);
            component.heal(BigDecimal.valueOf(damageResult.getDamage()).multiply(new BigDecimal("0.6")).setScale(0, RoundingMode.CEILING).intValue());
            return 0;
        });
        return basicSkill;
    }
}
